{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "d72b13a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import math\n",
    "from sklearn.metrics import r2_score\n",
    "\n",
    "#随机森林分类\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "#线性回归\n",
    "from sklearn.linear_model import LinearRegression as LR\n",
    "#岭回归\n",
    "from sklearn.linear_model import Ridge\n",
    "#三次多项式回归\n",
    "#quadratic polynomial 二次多项式回归\n",
    "from sklearn.preprocessing import PolynomialFeatures as PF\n",
    "\n",
    "#评估指标\n",
    "from sklearn.metrics import accuracy_score \n",
    "%matplotlib inline\n",
    "\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.model_selection import cross_val_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f0d105c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 4421 entries, 0 to 4420\n",
      "Data columns (total 6 columns):\n",
      " #   Column  Non-Null Count  Dtype  \n",
      "---  ------  --------------  -----  \n",
      " 0   label   4421 non-null   int64  \n",
      " 1   A       4419 non-null   float64\n",
      " 2   B       4421 non-null   float64\n",
      " 3   C       4421 non-null   float64\n",
      " 4   D       4421 non-null   float64\n",
      " 5   E       4421 non-null   float64\n",
      "dtypes: float64(5), int64(1)\n",
      "memory usage: 207.4 KB\n"
     ]
    }
   ],
   "source": [
    "data = pd.read_excel(\"data.xls\"\n",
    "                  #,index_col=0\n",
    "                  )\n",
    "data.head()\n",
    "data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "72c28615",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4419, 6)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#有空值，删除该列\n",
    "data.isnull().sum()\n",
    "data.shape\n",
    "data = data.dropna()\n",
    "data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "4b7307c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   label\n",
       "0     14\n",
       "1     17\n",
       "2      4\n",
       "3     18\n",
       "4      7"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#分割数据集并重排序号\n",
    "x = data.iloc[:,data.columns !='label']\n",
    "y = data.iloc[:,data.columns =='label']\n",
    "Xtrain,Xtest,Ytrain,Ytest=train_test_split(x,y,test_size=0.3)\n",
    "for i in [Xtrain,Xtest,Ytrain,Ytest]:\n",
    "    i.index=range(i.shape[0])\n",
    "\n",
    "Ytrain.head()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df2ca9e4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "e8702fda",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'DataFrame' object has no attribute 'ravel'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-44-b89bb19214a4>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mYtrain\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mYtrain\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mravel\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mYtrain\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mravel\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mD:\\Anaconda\\lib\\site-packages\\pandas\\core\\generic.py\u001b[0m in \u001b[0;36m__getattr__\u001b[1;34m(self, name)\u001b[0m\n\u001b[0;32m   5485\u001b[0m         ):\n\u001b[0;32m   5486\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 5487\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mobject\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__getattribute__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   5488\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   5489\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m__setattr__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mname\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m->\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'DataFrame' object has no attribute 'ravel'"
     ]
    }
   ],
   "source": [
    "type(Ytrain)\n",
    "np.array(Ytrain)\n",
    "np.array(Ytrain).ravel().shape\n",
    "Ytrain.ravel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "870a6680",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.058823529411764705"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#线性回归\n",
    "reg=LR().fit(Xtrain,Ytrain)\n",
    "ypre_reg = reg.predict(Xtest)\n",
    "#r2_score(Ytest,ypre)\n",
    "#四舍五入\n",
    "ypre_reg = np.around(ypre_reg)\n",
    "#类型转换\n",
    "ypre_reg_r = ypre_reg.astype(np.int64)\n",
    "# for i in range(len(yhat)):\n",
    "#     print(yhat[i])\n",
    "#     print(Ytest.iloc[i])\n",
    "#计算得分\n",
    "accuracy_score(Ytest,ypre_reg_r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "a5382918",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5384615384615384"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#随机森林分类\n",
    "rfc = RandomForestClassifier(random_state=0)\n",
    "rfc = rfc.fit(Xtrain,np.array(Ytrain).ravel())\n",
    "ypre_rfc = rfc.predict(Xtest)\n",
    "\n",
    "accuracy_score(Ytest,ypre_rfc)\n",
    "#score_r = rfc.score(Xtest,Ytest)\n",
    "#cross_val_score(rfc,x,np.array(y).ravel(),cv=10,scoring='neg_mean_squared_error')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "e93c1c7e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.06259426847662142"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#岭回归\n",
    "rid = Ridge(alpha=5).fit(Xtrain,Ytrain)\n",
    "ypre_rid = rid.predict(Xtest)\n",
    "#四舍五入\n",
    "ypre_rid_r = np.around(ypre_rid)\n",
    "#类型转换\n",
    "ypre_rid_r = ypre_rid.astype(np.int64)\n",
    "accuracy_score(Ytest,ypre_rid_r)\n",
    "#岭回归加的一点正则化项对结果没啥影响"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "8eeafc16",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.06108597285067873 0.08295625942684766\n"
     ]
    }
   ],
   "source": [
    "#二，三，多项式回归\n",
    "xtrain_p2 = PF(degree=2,include_bias=False).fit_transform(Xtrain)\n",
    "xtrain_p3 = PF(degree=3,include_bias=False).fit_transform(Xtrain)\n",
    "\n",
    "xtest_p2 = PF(degree=2,include_bias=False).fit_transform(Xtest)\n",
    "xtest_p3 = PF(degree=3,include_bias=False).fit_transform(Xtest)\n",
    "Linear2 = LR().fit(xtrain_p2,Ytrain)\n",
    "Linear3 = LR().fit(xtrain_p3,Ytrain)\n",
    "#四舍五入\n",
    "ypre_p2 = Linear2.predict(xtest_p2)\n",
    "ypre_p3 = Linear3.predict(xtest_p3)\n",
    "\n",
    "ypre_p2_r = np.around(ypre_p2)\n",
    "ypre_p3_r = np.around(ypre_p3)\n",
    "#类型转换\n",
    "ypre_p2_r = ypre_p2_r.astype(np.int64)\n",
    "score2 = accuracy_score(Ytest,ypre_p2_r)\n",
    "ypre_p3_r = ypre_p3_r.astype(np.int64)\n",
    "score3 = accuracy_score(Ytest,ypre_p3_r)\n",
    "print(score2,score3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c5ce05e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e48dcef5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
